using System;
using System.Collections;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData
{
	//*********************************************************************
	//
	// XMLUsers Class
	//
	// The XMLUsers class represents a collection base of XMLUser
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XMLUsers Has no fields
	//
	//*********************************************************************
	public class XMLUsers : bOS.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "Users";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLUsers()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLUsers(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/
		override public void Init()
		{
			this.RemoveAll();
		}

		/**Method to parse an xml string into a nodelist collection **/
		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLUser.MAIN_NODE:
						this.Add ( new XMLUser(node));
						break;
				}
			}
		}

		/**Method to retive a nodelist from collection **/
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLUsers.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Key,
			Name,
			DisplayName,
			Email
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Key:
					InnerList.Sort(new KeyComparer());
					break;
				case SortFields.Name:
					InnerList.Sort(new NameComparer());
					break;
				case SortFields.DisplayName:
					InnerList.Sort(new DisplayNameComparer());
					break;
				case SortFields.Email:
					InnerList.Sort(new EmailComparer());
					break;
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class NameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLUser first = (XMLUser) x;
				XMLUser second = (XMLUser) y;
				return first.Name.CompareTo(second.Name);
			}
		}

		private sealed class KeyComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLUser first = (XMLUser) x;
				XMLUser second = (XMLUser) y;
				return first.Key.CompareTo(second.Key);
			}
		}

		private sealed class DisplayNameComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLUser first = (XMLUser) x;
				XMLUser second = (XMLUser) y;
				return first.DisplayName.CompareTo(second.DisplayName);
			}
		}

		private sealed class EmailComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLUser first = (XMLUser) x;
				XMLUser second = (XMLUser) y;
				return first.Email.CompareTo(second.Email);
			}
		}

		/** method to retive a single role from name **/
		public XMLUser GetUser(string sKey)
		{
			for ( int i=0; i<this.Count;i++)
			{
				XMLUser user = (XMLUser) this.List[i];

				if ( user.Key  == sKey )
					return user;
			}

			throw new Exception.UserNotFound("User not found");
		}
	}
}